﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using NicoView.Model.Speech.Learn;
using NicoView.Model.Service;

namespace NicoView.Model.Speech
{
    public abstract class SpeacherBase<L>:ISpeacher<L>
        where L:ILearnModel,new()
    {

        public SpeacherBase(RegexLearnDictionary regexDictionary,LearnDictionary dictinoary)
            : this()
        {

            LearnModel.RegexLearnDictionary = regexDictionary;

            LearnModel.LearnDictionary = dictinoary;

        }

        private SpeacherBase()
        {
            LearnModel = new L();
        }

        public void Speech(SynthesizeArgs args)
        {

            if (args.LearnRequest)
            {
                var checkResult = LearnModel.CheckTextCommand(args.Body);
                if (checkResult != "")
                {
                    Speech(new SynthesizeArgs(checkResult, "", false));
                    return;
                }
            }
            
            if (args.ConvertRequest)
            {
                

                args.Body = this.LearnModel.Convert(args.Body);
                args.Name = this.LearnModel.Convert(args.Name);
            }

            args.Body = _htmlTagRegex.Replace(args.Body, "");

            Synthesize(args);
        }

        protected abstract void Synthesize(SynthesizeArgs args);

        public abstract void SpeechCancel();

        public L LearnModel
        {
            get;
            private set;
        }




        public abstract void Dispose();


        public abstract string Name { get; }

        private static readonly Regex _htmlTagRegex = new Regex(NicoFormat.HTML_TAG);
    }
}
